<?php
error_reporting(E_ALL); 
if (isset($_FILES['tabulato_elise']) && isset($_FILES['tabulato_vecchia'])) {
	$start = microtime();
	//print_r($_FILES);
	if ( (strlen($_FILES['tabulato_elise']['name']) > 0 ) && ( file_exists($_FILES['tabulato_elise']['tmp_name']) ) ) {
		echo 'trovato tabulato elise ' . $_FILES['tabulato_elise']['name'] . '<br />';
		$elise_microtime_start = microtime();
		$codiciProdotto = array('CHCOP', 'CHCAS', 'CHORD', 'MTCOP', 'MTORD', 'MTCAS', 'PERSO', 'TRM  ');
		$voci = array();
		$i = 0;
		
		$totali = array('mora' => 0);
		$linee = file($_FILES['tabulato_elise']['tmp_name']);
		
		$csv = fopen('tabulato.csv', 'w');
		fwrite($csv, 'filiale;num_fin;anagrafica;scadenza_rata;progressivo_rata;importo_rata;capitale;interessi;spese;mora' . "\n");
		//fwrite($csv, '23');
		
		foreach ($linee AS $line => $lineaTabulato){
			if ( preg_match('%^(?P<filiale>\d{0,5})\s*?(?P<num_fin>\d{6,8})\s*?(?P<anagrafica>.*?)\s(?P<scadenza_rata>\d\d\/\d\d\/\d\d\d\d)\s*?(?P<progressivo_rata>\d{1,3})\s*?Eur\s*?(?P<importo_rata>[\d.,]+)\s*?(?P<capitale>[\d.,]+)\s*?(?P<interessi>[\d.,]+)\s*?(?P<spese>[\d.,]+)\s*?(?P<mora>[\d.,]+)%', $lineaTabulato, $campi)) {
				//print_r($campi);
				//die();
				//$totali['mora'] += tabulati2flloat($campi['mora']);
				$string= $campi['filiale'] . ';' . $campi['num_fin'] . ';"' . trim($campi['anagrafica']) . '";' . $campi['scadenza_rata'] . ';' . $campi['progressivo_rata'] . ';' . $campi['importo_rata'] . ';' . $campi['capitale'] . ';' . $campi['interessi'] . ';' . $campi['spese'] . ';' . $campi['mora'] . "\n";
				fwrite($csv, $string);
			}
		}
		echo 'fine foreach';
		fclose($csv);
		
		echo 'Elaborazione Tabulato Elise: ' . number_format(diff_microtime($elise_microtime_start, microtime()), 4). ' secondi<br /><br />';
		html_show_array($totali);
	}
	
	echo 'Elaborazione Totale: ' . number_format(diff_microtime($start, microtime()), 4). ' secondi<br /><br />';
}
else {
?>
<form action="" method="post" enctype="multipart/form-data">
	<p>Tabulato Elise:
	<input type="file" name="tabulato_elise" />
	<br />
	Tabulato Vecchia:
	<input type="file" name="tabulato_vecchia" />
	<input type="submit" />
		</p>
</form>
<?php
}

function tabulati2flloat ($campo) {
	
	$originali = array('.', ',');
	$sostituzioni = array('', '.');
	
	return (float) trim(str_replace($originali, $sostituzioni, $campo));
	
}

function do_offset($level){
    $offset = "";             // offset for subarry 
    for ($i=1; $i<$level;$i++){
    $offset = $offset . "<td></td>";
    }
    return $offset;
}

function show_array($array, $level, $sub){
    if (is_array($array) == 1){          // check if input is an array
       foreach($array as $key_val => $value) {
			if (is_float($value))
				$value = number_format($value, 2, ',', '.');
           $offset = "";
           if (is_array($value) == 1){   // array is multidimensional
           echo "<tr>";
           $offset = do_offset($level);
           echo $offset . "<td>" . $key_val . "</td>";
           show_array($value, $level+1, 1);
           }
           else{                        // (sub)array is not multidim
           if ($sub != 1){          // first entry for subarray
               echo "<tr nosub>";
               $offset = do_offset($level);
           }
           $sub = 0;
           echo $offset . "<td main ".$sub." width=\"120\">" . $key_val . 
               "</td><td width=\"120\">" . $value . "</td>"; 
           echo "</tr>\n";
           }
       } //foreach $array
    }  
    else{ // argument $array is not an array
        return;
    }
}

function html_show_array($array){
  echo "<table cellspacing=\"0\" border=\"2\">\n";
  show_array($array, 1, 0);
  echo "</table>\n";
}

function diff_microtime($mt_old,$mt_new) {
  list($old_usec, $old_sec) = explode(' ',$mt_old);
  list($new_usec, $new_sec) = explode(' ',$mt_new);
  $old_mt = ((float)$old_usec + (float)$old_sec);
  $new_mt = ((float)$new_usec + (float)$new_sec);
  return $new_mt - $old_mt;
}


?>